<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 字段设置数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\service\DataSourceService;
use common\service\FieldService;
use think\Exception;
use think\Request;

class FieldController extends VerifyController
{
    /**
     * @var \common\service\FieldService
     */
    protected $fieldService;

    public function __construct()
    {
        parent::__construct();
        $this->fieldService = FieldService::getInstance();
    }

    /**
     * 添加自定义字段
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function create(Request $request)
    {
        $param = $request->param();
        $resData = $this->fieldService->createCustomField($param['data']);
        return json(success_response('Custom field created successfully.', $resData));
    }

    /**
     * 修改自定义字段
     * @param Request $request
     * @return \think\Response|void
     */
    public function update(Request $request)
    {

    }

    /**
     * 获取系统所有表列表
     * @param Request $request
     * @return \think\Response
     */
    public function getTableList(Request $request)
    {
        $resData = $this->fieldService->getTableList();
        return json(success_response('Query was successful.', $resData));
    }


    /**
     * 获取指定表字段配置
     * @param Request $request
     * @return \think\Response
     */
    public function getFieldConfig(Request $request)
    {
        $param = $request->param();
        $tableName = $param['param']['filter']['table_name'];
        $resData = $this->fieldService->getFieldConfig($tableName);
        return json(success_response('Query was successful.', $resData));
    }

    /**
     * 保存字段配置
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function modifyFieldConfig(Request $request)
    {
        $param = $request->param();
        $tableName = $param['param']['filter']['table_name'];
        $resData = $this->fieldService->modifyFieldConfig($tableName, $param['data']);
        return json(success_response('The data table field configuration was modified successfully. ', $resData));
    }

    /**
     * 获取自定义字段类型列表
     * @param Request $request
     * @return \think\Response
     */
    public function getCustomFieldTypeList(Request $request)
    {
        $resData = $this->fieldService->getCustomFieldTypeList();
        return json(success_response('', $resData));
    }


    /**
     * 获取自定义下拉框可以关联模块列表数据
     * @param Request $request
     * @return \think\Response
     */
    public function getSelectModuleList(Request $request)
    {
        $resData = $this->fieldService->getSelectModuleList();
        return json(success_response('', $resData));
    }

    /**
     * 获取自定义水平可以关联模块列表数据
     * @param Request $request
     * @return \think\Response
     */
    public function getHorizontalModuleList(Request $request)
    {
        $resData = $this->fieldService->getHorizontalModuleList();
        return json(success_response('', $resData));
    }

    /**
     * 获取字段的数据源
     * @param Request $request
     * @return \think\Response
     */
    public function getFieldDataSource(Request $request)
    {
        $param = $request->param();
        $dataSourceService = new DataSourceService();
        $resData = $dataSourceService->getFieldDataSource($param['data_source_name']);
        return json(success_response('', $resData));
    }

    /**
     * 自动刷新字典配置和缓存
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function autoFixFieldConfigAndCache(Request $request)
    {
        $this->fieldService->autoFixFieldConfigAndCache();
        return json(success_response(''));
    }

    /**
     * 输出模块的字段信息 md格式
     * @param Request $request
     * @return string
     */
    public function outputModuleTableFieldsWithFormat(Request $request)
    {
        $param = $request->param()['param'];
        return $this->fieldService->outputModuleTableFieldsWithFormat($param);
    }

    /**
     * 查询excel导入字段
     * @param Request $request
     * @return \think\Response
     * @throws Exception
     */
    public function getExcelImportFields(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->fieldService->getExcelImportFields($param["module_code"]);
        return json(success_response('', $res));

    }
}
